package video_utils

import (
	"cloud_android_backend/pkg/models/video"
	video2 "cloud_android_backend/pkg/modules/video"
	"modules.tenxcloud.com/common/composite"
	"os"
	// "github.com/gin-gonic/gin"
	"k8s.io/klog/v2"
)

// HandleVideoProcessing 回调函数，处理视频转码和特征提取
func HandleVideoProcessing(taskID string, videoName string, tempFilePath string, file_path string, fileNameArr []string, RelativeVideoFilePath string, userID string, isPublic bool) {
	// 确保在函数退出时删除临时文件
	defer func() {
		if err := os.Remove(tempFilePath); err != nil {
			klog.Error("删除临时文件失败: ", err)
		}
	}()
	// 调用函数将视频文件转码
	err := TranscodeVideo(tempFilePath, file_path)
	if err != nil {
		klog.Error("文件格式不合法或转码失败: ", err)
		return
	}
	// 获取数据库ORM对象
	db, err := composite.Database.GetOrm()
	if err != nil {
		klog.Error("获取数据库 ORM 对象失败: ", err)
		return
	}
	videoModel := video.Videos{
		ID: taskID,
	}
	err = videoModel.GetById(db)
	if err != nil {
		klog.Error("从数据库中获取刚插入的记录失败: ", err)
		return
	}
	// 初始化视频处理器，用于处理特征提取任务
	videoProcessor := video2.VideoDetectProcessor(db)
	_, err = videoProcessor.Client.ProcessVideo(taskID, RelativeVideoFilePath)
	if err != nil {
		klog.Error("视频处理失败: ", err)
		videoProcessor.UpdateVideoStatus(&videoModel, video.MakeStatusFailed, "视频处理失败")
		return
	}
	// 开始轮询视频处理状态，直到处理完成
	videoProcessor.PollingVideoProcessingStatus(taskID, &videoModel)
}
